package com.video.user.controller;


import com.video.common.enumeration.AppHttpCodeEnum;
import com.video.common.result.ResponseResult;
import com.video.common.utils.UserContext;
import com.video.user.domain.dto.LoginDto;
import com.video.user.domain.entity.AppUser;
import com.video.user.mapper.FriendShipsMapper;
import com.video.user.service.IAppUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * app用户表 前端控制器
 * </p>
 *
 * @author la
 * @since 2024-11-04
 */
@RestController
@RequestMapping("/app-user")
@Api(tags = "app用户管理")
@RequiredArgsConstructor
public class AppUserController {

    // 导入登录服务
    private final IAppUserService appUserService;
    private final FriendShipsMapper friendShipsMapper;

    /*
     * 登录接口
     * */
    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    public ResponseResult login(@RequestBody LoginDto loginDto) {
        ResponseResult result = appUserService.login(loginDto);
        return result;
    }

    /*
     * 用户信息
     * */
    @GetMapping("/info")
    public ResponseResult info() {
        // 根据网关和拦截器信息获取用户id
        Long appUserId = UserContext.getUser();
        // 查询id对用的信息
        AppUser user = appUserService.lambdaQuery().eq(AppUser::getId, appUserId).one();
        return ResponseResult.okResult(user);
    }

    /*
     * 更新用户信息
     * */
    @PostMapping("/update")
    public ResponseResult updateAppUser(@RequestBody AppUser user) {
        System.out.println(user);
        appUserService.updateUser(user);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 获取好友列表
     * @param userId
     * @return
     */
    @GetMapping("/friendList/{userId}")
    public ResponseResult friendList(@PathVariable  Long userId){
        List<AppUser> list= friendShipsMapper.getAcceptedFriends(userId);
        return ResponseResult.okResult(list) ;
    }

}
